home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / htttrack / httrack-3.41-3.exe / {app} / src / webhttrack < prev    next >
Text File  |  2007-01-14  |  6KB  |  187 lines

  1. #!/bin/bash
  2. #
  3. # WebHTTrack launcher script
  4. # Initializes the htsserver GUI frontend and launch the default browser
  5.  
  6. BROWSEREXE=
  7. SRCHBROWSEREXE="x-www-browser www-browser iceape mozilla firefox firebird galeon konqueror opera netscape"
  8. if test -n "${BROWSER}"; then
  9. # sensible-browser will f up if BROWSER is not set
  10. SRCHBROWSEREXE="sensible-browser ${SRCHBROWSEREXE}"
  11. fi
  12. SRCHPATH="/usr/local/bin /usr/share/bin /usr/bin /usr/lib/httrack /usr/local/lib/httrack /usr/local/share/httrack /sw/bin ${HOME}/usr/bin ${HOME}/bin"
  13. SRCHPATH="$SRCHPATH "`echo $PATH | tr ":" " "`
  14. SRCHDISTPATH="/usr/share /usr/local /usr /local /usr/local/share ${HOME}/usr ${HOME}/usr/share /sw ${HOME}/usr/local ${HOME}/usr/share"
  15.  
  16. ###
  17. # And now some famous cuisine
  18.  
  19. function log {
  20. echo "$0($$): $@" >&2
  21. return 0
  22. }
  23.  
  24. function mozillabrowser {
  25. # returns 0, if the browser is mozilla type
  26. echo "$1" | grep -q "iceape"
  27. [ $? -eq 0 ] && return 0 
  28. echo "$1" | grep -q "mozilla"
  29. [ $? -eq 0 ] && return 0 
  30. echo "$1" | grep -q "netscape"
  31. [ $? -eq 0 ] && return 0 
  32. echo "$1" | grep -q "firebird"
  33. [ $? -eq 0 ] && return 0 
  34. echo "$1" | grep -q "firefox"
  35. [ $? -eq 0 ] && return 0 
  36. return 1;
  37. }
  38. function mozillaloaded  {
  39. user_name=`logname 2>/dev/null`
  40. if ! test -n "${user_name}"; then
  41. user_name=`id -un`
  42. fi
  43. if test -n "${user_name}"; then
  44. ps -e --user "$user_name" | grep -qE "(iceape|mozilla|netscape|firebird|firefox)"
  45. else
  46. false
  47. fi
  48. }
  49.  
  50. function launch_browser {
  51. log "launching $1"
  52. start_t=`date +%s`
  53. browser=$1
  54. url=$2
  55. moz=
  56. if mozillaloaded; then
  57. moz=1
  58. fi
  59. # launch any browser
  60. # if it is a mozilla like browser, check if the browser is running and use 
  61. # -remote if needed. Change the URL into openURL($url) too. 
  62. # (thanks to Torsten Werner for the patch)
  63. # see http://www.mozilla.org/unix/remote.html
  64. # 04/2006: openurl() fix from Samuel Suther
  65. if mozillabrowser ${browser}; then
  66.     if ! ${browser} -remote "openurl(${url})"; then
  67.         log "spawning browser.."
  68.         ${browser} "${url}"
  69.     fi
  70. else
  71.     log "spawning regular browser.."
  72.     ${browser} "${url}"
  73. fi
  74. # this is a real pain in the neck: browser can hiddenly use the -remote feature of
  75. # mozilla and therefore return immediately
  76. # this loop is the only reliable solution AFAIK
  77. end_t=`date +%s`
  78. if test -n "$start_t" -a -n "$end_t"; then
  79.     int_t=$[$end_t-$start_t]
  80. else
  81.     int_t=0
  82. fi
  83. if test -n "${int_t}" -a "${int_t}" -lt 60; then
  84.     if test -n "$moz"; then
  85.         log "waiting for browser to terminate.."
  86.         while mozillaloaded; do
  87.             sleep 3
  88.         done
  89.         log "browser seems to have been closed.."
  90.     fi
  91. fi
  92. log "browser exited"
  93. }
  94.  
  95. # First ensure that we can launch the server
  96. BINPATH=
  97. for i in ${SRCHPATH}; do
  98.     ! test -n "${BINPATH}" && test -x ${i}/htsserver && BINPATH=${i}
  99. done
  100. for i in ${SRCHDISTPATH}; do
  101.     ! test -n "${DISTPATH}" && test -f "${i}/httrack/lang.def" && DISTPATH="${i}/httrack"
  102. done
  103. test -n "${BINPATH}" || ! log "could not find htsserver" || exit 1
  104. test -n "${DISTPATH}" || ! log "could not find httrack directory" || exit 1
  105. test -f ${DISTPATH}/lang.def || ! log "could not find ${DISTPATH}/lang.def" || exit 1
  106. test -f ${DISTPATH}/lang.indexes || ! log "could not find ${DISTPATH}/lang.indexes" || exit 1
  107. test -d ${DISTPATH}/lang || ! log "could not find ${DISTPATH}/lang" || exit 1
  108. test -d ${DISTPATH}/html || ! log "could not find ${DISTPATH}/html" || exit 1
  109.  
  110. # Locale
  111. HTSLANG="${LC_MESSAGES}"
  112. ! test -n "${HTSLANG}" && HTSLANG="${LC_ALL}"
  113. ! test -n "${HTSLANG}" && HTSLANG="${LANG}"
  114. test -n "${HTSLANG}" && HTSLANG="`echo ${HTSLANG} | cut -c1-2` | tr 'A-Z' 'a-z'"
  115. LANGN=`grep "${HTSLANG}:" ${DISTPATH}/lang.indexes | cut -f2 -d':'`
  116. ! test -n "${LANGN}" && LANGN=1
  117.  
  118. # Find the browser
  119. # note: not all systems have sensible-browser or www-browser alternative
  120. # thefeore, we have to find a bit more if sensible-browser could not be found
  121.  
  122. for i in ${SRCHBROWSEREXE}; do
  123. for j in ${SRCHPATH}; do
  124. if test -x ${j}/${i}; then
  125. BROWSEREXE=${j}/${i}
  126. fi
  127. test -n "$BROWSEREXE" && break
  128. done
  129. test -n "$BROWSEREXE" && break
  130. done
  131. test -n "$BROWSEREXE" || ! log "cound not find any suitable browser" || exit 1
  132.  
  133. # "browse" command
  134. if test "$1" = "browse"; then
  135. if test -f "${HOME}/.httrack.ini"; then
  136. INDEXF=`cat ${HOME}/.httrack.ini | tr '\r' '\n' | grep -E "^path=" | cut -f2- -d'='`
  137. if test -n "${INDEXF}" -a -d "${INDEXF}" -a -f "${INDEXF}/index.html"; then
  138. INDEXF="${INDEXF}/index.html"
  139. else
  140. INDEXF=""
  141. fi
  142. fi
  143. if ! test -n "$INDEXF"; then 
  144. INDEXF="${HOME}/websites/index.html"
  145. fi
  146. launch_browser "${BROWSEREXE}" "file://${INDEXF}"
  147. exit $?
  148. fi
  149.  
  150. # Create a temporary filename
  151. TMPSRVFILE="/tmp/.webhttrack.$$.`head -c16 /dev/random | md5sum | cut -f1 -d' '`"
  152. >${TMPSRVFILE} || ! log "cound not create the temporary file ${TMPSRVFILE}" || exit 1
  153. # Launch htsserver binary and setup the server
  154. (${BINPATH}/htsserver "${DISTPATH}/" path "${HOME}/websites" lang "${LANGN}" $@; echo SRVURL=error) > ${TMPSRVFILE}&
  155. # Find the generated SRVURL
  156. SRVURL=
  157. MAXCOUNT=60
  158. while ! test -n "$SRVURL"; do
  159. MAXCOUNT=$[$MAXCOUNT - 1]
  160. test $MAXCOUNT -gt 0 || exit 1
  161. test $MAXCOUNT -lt 50 && echo "waiting for server to reply.."
  162. SRVURL=`grep -E URL= ${TMPSRVFILE} | cut -f2- -d=`
  163. test ! "$SRVURL" = "error" || ! log "could not spawn htsserver" || exit 1
  164. test -n "$SRVURL" || sleep 1
  165. done
  166.  
  167. # Cleanup function
  168. function cleanup {
  169. test -n "$1" && log "nasty signal caught, cleaning up.."
  170. test -f ${TMPSRVFILE} && SRVPID=`grep -E PID= ${TMPSRVFILE} | cut -f2- -d=`
  171. test -n "${SRVPID}" && kill -9 ${SRVPID}
  172. test -f ${TMPSRVFILE} && rm ${TMPSRVFILE}
  173. test -n "$1" && log "..done"
  174. return 0
  175. }
  176.  
  177. # Cleanup in case of emergency
  178. trap "cleanup now; exit" 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25
  179.  
  180. # Got SRVURL, launch browser
  181. launch_browser "${BROWSEREXE}" "${SRVURL}"
  182.  
  183. # That's all, folks!
  184. trap "" 1 2 3 4 5 6 7 8 9 11 13 14 15 16 19 24 25
  185. cleanup
  186. exit 0
  187.